﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.LookAndFeel;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using DevExpress.Utils.Svg.Extensions;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 主题外观设置页
    /// </summary>
    public partial class FrmSettingThemeForm : BaseForm
    {
        public FrmSettingThemeForm()
        {
            InitializeComponent();
        }

        private void SetControlStyle()
        {
            groupSkinStyle.BackColor = groupUIStyle.BackColor = BackColor;
            groupSkinStyle.ForeColor = groupUIStyle.ForeColor = ForeColor;
        }

        private void SetGalleryVisible()
        {
            var isSkinStyle = UserLookAndFeel.Default.ActiveStyle == ActiveLookAndFeelStyle.Skin;
            layoutItemGalleryTheme.Enabled = isSkinStyle;
            layoutItemPalette.Visibility =
                emptytemPalette.Visibility = WinFormHelper.LayoutItemVisible(isSkinStyle && UserLookAndFeel.Default.IsSvgSkin());
        }

        private void FrmNewTheme_Load(object sender, EventArgs e)
        {
            if (WinFormHelper.IsDesignMode()) return;
            UserLookAndFeel.Default.StyleChanged += Default_StyleChanged;

            SkinHelper.InitSkinGallery(galleryTheme, true, true);
            SkinHelper.InitSkinPaletteGallery(editSvgPalette);

            groupSkinStyle.Properties.Items.BeginUpdate();
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("Skin", "主题样式"));
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("Office2003", "Office样式"));
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("Native", "原生样式"));
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("Style3D", "立体样式"));
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("Flat", "扁平样式"));
            groupSkinStyle.Properties.Items.Add(new RadioGroupItem("UltraFlat", "扁平增强样式"));
            groupSkinStyle.Properties.Items.EndUpdate();

            groupUIStyle.Properties.Items.BeginUpdate();
            groupUIStyle.Properties.Items.Add(new RadioGroupItem("DefaultUI", "默认界面"));
            groupUIStyle.Properties.Items.Add(new RadioGroupItem("CompactUI", "紧凑界面"));
            groupUIStyle.Properties.Items.Add(new RadioGroupItem("TouchUI", "触摸界面"));
            groupUIStyle.Properties.Items.EndUpdate();

            var themeConfig = ThemeConfig.Get();
            editCustomFont.Text = ObjectHelper.GetObjectString(WindowsFormsSettings.DefaultFont);
            groupUIStyle.EditValue = themeConfig.UserInterfaceMode;
            editUseDirectX.IsOn = themeConfig.UseDirectX;
            editUseDpiAware.IsOn = themeConfig.UseDpiAware;
            groupSkinStyle.EditValue = themeConfig.Style;
            editSvgPalette.EditValue = themeConfig.SvgPalette;

            SetControlStyle();
            SetGalleryVisible();
        }

        private void Default_StyleChanged(object sender, EventArgs e)
        {
            SetControlStyle();
            SetGalleryVisible();
        }

        private void GroupSkinStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (groupSkinStyle.EditValue == null) return;

            string style = groupSkinStyle.EditValue.ToString();
            switch (style)
            {
                case "Skin":
                    UserLookAndFeel.Default.SetSkinStyle(UserLookAndFeel.Default.ActiveSkinName, UserLookAndFeel.Default.ActiveSvgPaletteName);
                    break;
                case "Native":
                    UserLookAndFeel.Default.SetWindowsXPStyle();
                    break;
                case "Style3D":
                    UserLookAndFeel.Default.SetStyle3D();
                    break;
                case "Office2003":
                    UserLookAndFeel.Default.SetOffice2003Style();
                    break;
                case "Flat":
                    UserLookAndFeel.Default.SetFlatStyle();
                    break;
                case "UltraFlat":
                    UserLookAndFeel.Default.SetUltraFlatStyle();
                    break;
            }
            SetControlStyle();
            SetGalleryVisible();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var themeSetting = ThemeConfig.Get();
            themeSetting.Style = groupSkinStyle.EditValue.ToString();
            themeSetting.Skin = UserLookAndFeel.Default.ActiveSkinName;
            themeSetting.SvgPalette = UserLookAndFeel.Default.ActiveSvgPaletteName;
            themeSetting.CustomFont = ObjectHelper.GetObjectString(WindowsFormsSettings.DefaultFont);
            themeSetting.UserInterfaceMode = groupUIStyle.EditValue.ToString();
            themeSetting.UseDirectX = editUseDirectX.IsOn;
            themeSetting.UseDpiAware = editUseDpiAware.IsOn;
            ThemeConfig.Save();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void FrmNewTheme_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            UserLookAndFeel.Default.StyleChanged -= Default_StyleChanged;
        }

        private void editCustomFont_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            fontDialog1.Font = DevExpress.Utils.AppearanceObject.DefaultFont;
            if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var fontObj = fontDialog1.Font;
                editCustomFont.Text = ObjectHelper.GetObjectString(fontObj);
                WindowsFormsSettings.DefaultFont = fontObj;
                WindowsFormsSettings.DefaultMenuFont = fontObj;
                WindowsFormsSettings.DefaultPrintFont = fontObj;
            }
        }
    }
}